package com.situ.stmall.controller.api;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.situ.stmall.entity.APIResult;
import com.situ.stmall.entity.Shopcart;
import com.situ.stmall.entity.User;
import com.situ.stmall.service.ShopcartService;

@RestController
@RequestMapping("/api/cart")
public class APICartController {

	@Autowired
	private ShopcartService shopcartService;
	
	@GetMapping("/getAll")
	public List getAll(HttpSession session) {
		// 1. 参数
		User user = (User) session.getAttribute("user");
		
		// 2. 调用
		return shopcartService.getByUserId(user.getId());
		// 3. 返回
	}
	
	@GetMapping("/update")
	public APIResult update(Shopcart cart) {
		
		Shopcart selectCart = shopcartService.update(cart);
		
		if (selectCart.getCount() == cart.getCount()) {
			return APIResult.success(null, selectCart);
		} else {
			return APIResult.fail("库存不足, 修改失败", selectCart);
		}
	}
}
